home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / transaction / _manager.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  4.7 KB  |  150 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''A TransactionManager controls transaction boundaries.
  5.  
  6. It coordinates application code and resource managers, so that they
  7. are associated with the right transaction.
  8. '''
  9. import thread
  10. from transaction._transaction import Transaction
  11. _marker = object()
  12.  
  13. def _new_transaction(txn, synchs):
  14.     if synchs:
  15.         synchs.map((lambda s: s.newTransaction(txn)))
  16.     
  17.  
  18.  
  19. class TransactionManager(object):
  20.     
  21.     def __init__(self):
  22.         WeakSet = WeakSet
  23.         import ZODB.utils
  24.         self._txn = None
  25.         self._synchs = WeakSet()
  26.  
  27.     
  28.     def begin(self):
  29.         if self._txn is not None:
  30.             self._txn.abort()
  31.         
  32.         txn = self._txn = Transaction(self._synchs, self)
  33.         _new_transaction(txn, self._synchs)
  34.         return txn
  35.  
  36.     
  37.     def get(self):
  38.         if self._txn is None:
  39.             self._txn = Transaction(self._synchs, self)
  40.         
  41.         return self._txn
  42.  
  43.     
  44.     def free(self, txn):
  45.         if not txn is self._txn:
  46.             raise AssertionError
  47.         self._txn = None
  48.  
  49.     
  50.     def registerSynch(self, synch):
  51.         self._synchs.add(synch)
  52.  
  53.     
  54.     def unregisterSynch(self, synch):
  55.         self._synchs.remove(synch)
  56.  
  57.     
  58.     def commit(self, sub = _marker):
  59.         if sub is _marker:
  60.             sub = None
  61.         else:
  62.             deprecated37 = deprecated37
  63.             import ZODB.utils
  64.             deprecated37('subtransactions are deprecated; use transaction.savepoint() instead of transaction.commit(1)')
  65.         return self.get().commit(sub, deprecation_wng = False)
  66.  
  67.     
  68.     def abort(self, sub = _marker):
  69.         if sub is _marker:
  70.             sub = None
  71.         else:
  72.             deprecated37 = deprecated37
  73.             import ZODB.utils
  74.             deprecated37('subtransactions are deprecated; use sp.rollback() instead of transaction.abort(1), where `sp` is the corresponding savepoint captured earlier')
  75.         return self.get().abort(sub, deprecation_wng = False)
  76.  
  77.     
  78.     def savepoint(self, optimistic = False):
  79.         return self.get().savepoint(optimistic)
  80.  
  81.  
  82.  
  83. class ThreadTransactionManager(TransactionManager):
  84.     '''Thread-aware transaction manager.
  85.  
  86.     Each thread is associated with a unique transaction.
  87.     '''
  88.     
  89.     def __init__(self):
  90.         self._txns = { }
  91.         self._synchs = { }
  92.  
  93.     
  94.     def begin(self):
  95.         tid = thread.get_ident()
  96.         txn = self._txns.get(tid)
  97.         if txn is not None:
  98.             txn.abort()
  99.         
  100.         synchs = self._synchs.get(tid)
  101.         if synchs is None:
  102.             WeakSet = WeakSet
  103.             import ZODB.utils
  104.             synchs = self._synchs[tid] = WeakSet()
  105.         
  106.         txn = self._txns[tid] = Transaction(synchs, self)
  107.         _new_transaction(txn, synchs)
  108.         return txn
  109.  
  110.     
  111.     def get(self):
  112.         tid = thread.get_ident()
  113.         txn = self._txns.get(tid)
  114.         if txn is None:
  115.             synchs = self._synchs.get(tid)
  116.             if synchs is None:
  117.                 WeakSet = WeakSet
  118.                 import ZODB.utils
  119.                 synchs = self._synchs[tid] = WeakSet()
  120.             
  121.             txn = self._txns[tid] = Transaction(synchs, self)
  122.         
  123.         return txn
  124.  
  125.     
  126.     def free(self, txn):
  127.         tid = thread.get_ident()
  128.         if not txn is self._txns.get(tid):
  129.             raise AssertionError
  130.         del self._txns[tid]
  131.  
  132.     
  133.     def registerSynch(self, synch):
  134.         tid = thread.get_ident()
  135.         ws = self._synchs.get(tid)
  136.         if ws is None:
  137.             WeakSet = WeakSet
  138.             import ZODB.utils
  139.             ws = self._synchs[tid] = WeakSet()
  140.         
  141.         ws.add(synch)
  142.  
  143.     
  144.     def unregisterSynch(self, synch):
  145.         tid = thread.get_ident()
  146.         ws = self._synchs[tid]
  147.         ws.remove(synch)
  148.  
  149.  
  150.